//
//  MainViewController.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/9.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit


class MainViewController: UITabBarController {

    var welcomeView : WelcomeView?
    var newfeature : NewFeatureView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewController()
        // Do any additional setup after loading the view.
        setupDiscoverTabBarItem()
        
        print(UserAccount.shareUserAccount.isLogin)
        
        if UserAccount.shareUserAccount.isLogin
        {
            addNewFeatureViewOrWelcomeView()

        }
    
//        addNewFeatureViewOrWelcomeView()
    }

}
extension MainViewController
{
    /**
     创建自控制器 并添加入
     */
    private func addChildViewController()
    {
        let homeViewController = HomeViewController()
        self.addChildViewController(homeViewController  , title: "首页", imageName: "tabbar_home")
        
        
        let messageViewController = MessageViewController()
        self.addChildViewController(messageViewController , title: "消息", imageName: "tabbar_message_center")
        
        let placeHolderController = UIViewController()
        self.addChildViewController(placeHolderController, title: nil, imageName: nil)
        
        let discoverViewController = DiscoverViewController()
        self.addChildViewController(discoverViewController, title: "发现", imageName: "tabbar_discover")
        
        let profileViewController = ProfileViewController()
        self.addChildViewController(profileViewController, title: "我的", imageName: "tabbar_profile")
        
        
       
    }
    
    /**
     配置子控制器
     
     - parameter childController: 子控制器
     - parameter title:           自控制器标题
     - parameter imageName:       子控制器BarItem 图片
     */
    private func addChildViewController(childController: UIViewController , title : String? , imageName : String?)
    {
        if let title = title
        {
            childController.title = title
            
            childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.darkGrayColor()], forState: .Normal)
            
            childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : ThemeColor], forState: .Selected)
        }
        
        if let imageName = imageName
        {
            childController.tabBarItem.image = UIImage(named:imageName )?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            childController.tabBarItem.selectedImage = UIImage(named:"\(imageName )_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        }
        
        let navigation = UINavigationController(rootViewController: childController)
        navigation.navigationBar 
        addChildViewController(navigation)
        
    }
    
    /**
     设置tabBar中间的按钮
     */
    private func setupDiscoverTabBarItem()
    {
        let discoverItem = UIButton(image: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button", title : nil ,target: self, action: "compose")
        discoverItem.center = CGPointMake(screenWidth/2, tabBar.bounds.size.height/2)
        tabBar.addSubview(discoverItem)
        setTabBarShadow()
    }
  
    /**
     添加新特性或欢迎页面
     */
    func addNewFeatureViewOrWelcomeView()
    {
        if NSBundle.hasNewVersion()
        {
            newfeature = NewFeatureView()
            view.addSubview(newfeature!)
        }
        else
        {
            welcomeView = WelcomeView()
            view.addSubview(welcomeView!)
        }
    }
    /**
     设置tabBar上的分界线
     */
    func setTabBarShadow()
    {
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.sampleImage()
    }
    /**
     中间按钮点击事件
     */
    @objc private func compose()
    {
        print("heihei")
    }
}